/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示 ATIM 的 OnePulse 功能。
  *          每次ATIM_CH1(PB04)的下降沿触发启动后，延时6ms ATIM_CH3(PA03)
  *          输出一个脉宽为4ms的正脉冲信号。
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：短接 J25 (实现板载用户按键连接到ATIM_CH1_PB04)
  *                    请按下板载用户按键(PB04)触发ATIM启动
  *                    请用示波器观察PB04的波形
  *                    请用示波器观察PA03的波形
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_atim.h"
#include "sym32l010_Startkit_board.h"


/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
ATIM_HandleTypeDef hatim;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void ATIM_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    // 初始化系统时钟
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI8MHZ);

    // 初始化SysTick
    HAL_InitTick(8000000);

    // 初始化ATIM
    ATIM_Init();

    while (1)
    {

    }
}


/*******************************************************************************
 * @brief  ATIM初始化
 * @param  None
 * @retval None
 */
static void ATIM_Init(void)
{
    GPIO_InitTypeDef          GPIO_InitStruct;
    ATIM_OnePulse_InitTypeDef ATIM_OnePulseInitStruct;

    MemClr(&GPIO_InitStruct,         sizeof(GPIO_InitStruct));
    MemClr(&ATIM_OnePulseInitStruct, sizeof(ATIM_OnePulseInitStruct));

    // 使能外设时钟
    HAL_SYSCTRL_GPIOA_CLK_ENABLE();
    HAL_SYSCTRL_GPIOB_CLK_ENABLE();
    HAL_SYSCTRL_ATIM_CLK_ENABLE();

    // 配置PA03为ATIM_CH3的输出管脚
    AFx_ATIMCH3_PA03();
    GPIO_InitStruct.Pins = GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    // 配置PB04为ATIM_CH1的触发输入管脚
    AFx_ATIMCH1_PB04();
    GPIO_InitStruct.Pins = GPIO_PIN_4;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULL_UP;
    HAL_GPIO_Init(SYM_GPIOB, &GPIO_InitStruct);

    // 配置ATIM
    MemClr(&hatim, sizeof(hatim));

    hatim.Instance               = SYM_ATIM;
    hatim.Init.ClockDivision     = ATIM_CLOCKDIVISION_DIV1;
    hatim.Init.CounterMode       = ATIM_COUNTERMODE_UP;
    hatim.Init.Prescaler         = 8000 - 1;
    hatim.Init.Period            = 10 - 1;                            // 溢出周期10ms
    hatim.Init.RepetitionCounter = 0;
    hatim.Init.AutoReloadPreload = ATIM_AUTORELOAD_PRELOAD_DISABLE;
    HAL_ATIM_OnePulse_Init(&hatim, ATIM_OPMODE_SINGLE);               // 单脉冲模式

    // 配置CH3为输出，配置CH1为输入
    ATIM_OnePulseInitStruct.OCMode = ATIM_OCMODE_PWM2;                // PWM模式2
    ATIM_OnePulseInitStruct.Pulse  = 6;                               // 启动6ms后产生宽度为4ms的脉冲
    ATIM_OnePulseInitStruct.OCIdleState = ATIM_OCIDLESTATE_SET;
    ATIM_OnePulseInitStruct.OCPolarity = ATIM_OCPOLARITY_HIGH;
    ATIM_OnePulseInitStruct.ICFilter   = 0;
    ATIM_OnePulseInitStruct.ICPolarity = ATIM_ICPOLARITY_FALLING;
    ATIM_OnePulseInitStruct.ICSelection = ATIM_ICSELECTION_DIRECTTI;  // IC1映射到TI1上

    // 配置CH3输出OnePulse脉冲，CH1用于触发启动
    HAL_ATIM_OnePulse_ConfigChannel(&hatim, &ATIM_OnePulseInitStruct, ATIM_CHANNEL_3, ATIM_CHANNEL_1);

    HAL_ATIM_OnePulse_Start(&hatim, ATIM_CHANNEL_3);                  // 启动OnePulse
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
